package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import daomain.Countries;

public class CountryDao {
	/**
	 * 插入
	 * 
	 * @param countries
	 */

	public void insert(Countries countries) {
		String sql = "insert into countries values(?,?,?)";
		Connection conn = Util.getConnection();
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, countries.getCountry_id());
			ps.setString(2, countries.getCountry_name());
			ps.setInt(3, countries.getRegion_id());
			ps.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 查询
	 * @return
	 */
	public List<Countries> foundAll() {
		List<Countries> list = new ArrayList<>();
		String sql = "select * from countries where country_id='SH'";
		Connection conn = Util.getConnection();
		try {
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			while (rs.next()) {
				Countries c = new Countries();
				c.setCountry_id(rs.getString(1));
				c.setCountry_name(rs.getString(2));
				c.setRegion_id(rs.getInt(3));
				list.add(c);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 删除 
	 * @param id
	 */
	public void delete(String id) {
		String sql = "delete from countries where country_id='" + id + "'";
		Connection conn = Util.getConnection();
		try {
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 修改
	 */
	public void update(String id){
		String sql="update countries set country_name='BeiJing' where country_id='"+id+"'";
		Connection conn=Util.getConnection();
		try {
			PreparedStatement pst=conn.prepareStatement(sql);
			pst.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
